package com.mvc.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DruidUtil {
    private static DataSource data = null;

    static {
        InputStream is = DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties");
        Properties ppt = new Properties();
        try {
            ppt.load(is);
            data = DruidDataSourceFactory.createDataSource(ppt);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 用于从DBCP连接池中 获取一个连接
     *
     * @return DBCP连接池中的一个连接对象.
     */
    public static Connection getConnection() {
        try {
            return data.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void close(Connection con, SetResultBean bean) {
        if (bean != null) {
            close(con, bean.getPs(), bean.getRs());

        } else {

            close(con, null, null);
        }
    }

    public static void close(Connection con, IntResultBean bean) {
        if (bean != null) {
            close(con, bean.getPs(), null);
        } else {
            close(con, null, null);

        }
    }

    /**
     * 用于释放连接 , 执行环境 , 结果集 等资源
     *
     * @param conn   要释放的连接资源
     * @param state  要释放的执行环境资源
     * @param result 要释放的结果集资源
     */


    public static void close(Connection conn, Statement state, ResultSet result) {

        if (result != null) {
            try {
                result.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();

            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    //编写查询公共方法
    public static SetResultBean query(Connection con, String sql, Object[] params) throws SQLException {
        if (con != null) {
            PreparedStatement ps = con.prepareStatement(sql);//此处不要自己创建preparedStatement
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
            }
            ResultSet rs = ps.executeQuery();

            return new SetResultBean(rs, ps);
        } else {
            return null;
        }

    }

    //编写增删改公共方法
    public static IntResultBean update(Connection con, String sql, Object[] params) throws SQLException {
        if (con == null) return null;
        PreparedStatement ps = con.prepareStatement(sql);
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
        }
        return new IntResultBean(ps.executeUpdate(), ps);
    }


    public static class SetResultBean {

        private final ResultSet rs;
        private final PreparedStatement ps;


        public ResultSet getRs() {
            return rs;
        }


        public PreparedStatement getPs() {
            return ps;
        }


        public SetResultBean(ResultSet rs, PreparedStatement ps) {

            this.rs = rs;
            this.ps = ps;
        }
    }

    public static class IntResultBean {
        private final int result;
        private final PreparedStatement ps;


        public int getResult() {
            return result;
        }


        public PreparedStatement getPs() {
            return ps;
        }


        public IntResultBean(int result, PreparedStatement ps) {
            this.result = result;
            this.ps = ps;
        }
    }
}